#!/bin/bash

# Slackware build script for tkgames

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Note: I'm only packaging the games. The other stuff is:
# - alert, basically xmessage rewritten in tcl/tk.
# - tkexe, a "run command" dialog (we have plenty of those already).
# - mamex and sdlmamex, outdated frontends for outdated MAME ports.
#   modern MAME has its own frontend anyway.
# - the docs mention "Scid vs. PC" and "TkTimidity++", but these aren't
#   in the tarball.
# - also not packaging tksol/tksol_green (same game, different background).

# If I'm being 100% honest, I really only care about tkhearts. There
# aren't a lot of choices for playing Hearts on Linux, unless you want
# to play in a browser and look at ads.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=tkgames
VERSION=${VERSION:-1.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
ARCH=noarch

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKG/usr/games $PKG/usr/share/games $PKG/usr/man/man6 $PKGDOC

# cruft and hidden docs.
cp -a tksol/readme $PKGDOC/tksol.txt
cp -a tkhangman/README $PKGDOC/tkhangman.txt
gzip -9c < tksol/tksol.6 > $PKG/usr/man/man6/tksol.6.gz
rm -rf */.menus.swp tksol/{tksol.6,readme,install,tksol_green} \
       tkhangman/{extras,hangman,README,INSTALL}

# tkhearts logs too much to stdout, including spoilers (e.g. if one of
# the AIs decides to shoot the moon). squelch.
sed -i '/^[\t ]*puts  *"/s,^,#,' tkhearts-*

# These are self-contained executables, run from anywhere, no external files.
for i in tkhearts tkspider tktk tktp tkyahtzee; do
  cp -a $i-* $PKG/usr/games/$i
done

# These need external files, and have an install directory.
for i in polypuzzle tkhangman tksol; do
  sed -i 's,/usr/local/lib,/usr/share/games,' $i/$i
  mv $i/$i $PKG/usr/games/$i
  cp -a $i $PKG/usr/share/games/$i
done

# Some icons extracted from the tcl code (where they appear as
# base64'ed gifs), some were already external files. Tweaked them with
# the gimp to look good as 64x64.
mkdir -p $PKG/usr/share/icons/hicolor/64x64/apps $PKG/usr/share/pixmaps
install -oroot -groot -m0644 $CWD/icons/* $PKG/usr/share/icons/hicolor/64x64/apps
for i in $CWD/icons/*; do
  ln -s ../icons/hicolor/64x64/apps/$(basename $i) $PKG/usr/share/pixmaps
done

# .desktop files written for this SlackBuild.
mkdir -p $PKG/usr/share/applications
install -oroot -groot -m0644 $CWD/desktop/*.desktop $PKG/usr/share/applications

cp -a docs/index.{html,txt} docs/images $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
